<template>
	<view class="page__content">
		<HomeBoard :config="scrollConfig"></HomeBoard>
	</view>
</template>

<script setup lang="ts" name="pages-home">
	import { ref, computed, reactive, watch } from 'vue';
	import {
		onLoad,
		onShow
	} from "@dcloudio/uni-app";
	import { useMainStore, useInventoryStore, useUserStore, usePermissionStore } from '@/store';
	const inventoryStore = useInventoryStore();
	const mainStore = useMainStore();
	const userStore = useUserStore();
	const permissionStore = usePermissionStore();
	import useRequestQueue from '@/uni_modules/mc-ui/libs/http/UniversalRequestQueue';
	const { request, requestByUrl, onAllComplete, onError } = useRequestQueue();

	import HomeBoard from '@/commonPage/Module/home-board.vue';



	const props = defineProps({
		// 激活态颜色
		config: {
			type: Object,
			default: {
				scrollHeight: 500,
				tabbarHeight: 50
			}
		}
	})

	console.log(props.config)

	let scrollConfig = reactive<any>({
		scrollHeight: 500,
		tabbarHeight: 50
	})

	Object.assign(scrollConfig, props.config)
	console.log(scrollConfig.scrollHeight)
	// const scrollConfig = computed(() => {
	// 	return props.config;
	// })
	console.log(scrollConfig.value);
	watch(props.config, (nval : any) => {
		console.log(nval);
		Object.assign(scrollConfig, nval);
		console.log(scrollConfig.scrollHeight)
	}, {
		deep: true
	})

	// const scrollConfig = computed(() => {
	// 	return {
	// 		scrollHeight: props.config.height
	// 	}
	// })


	// 当前用户已开通模块
	let modules = ref<any>([]);
	// 当前所选版本
	let selectedCombo = ref<number>(-1);
	// 版本列表
	// let comboList = ref<any>([]);
	const comboList = computed(() => {
		return permissionStore.comboList
	}) as any;

	// 看板数据源
	let boardList = ref<any>([]);
	// 当前选中看板
	let selectedBoard = reactive<any>({
		boardID: 0,
		boardName: '',
		boardTitle: ''
	});
	let componentKey = ref<number>(0);


	function openBaseScreen(str : string) {

	};

	function changeCombo() {

	}


	let selectedComboInfo = ref<any>({});

	console.log(permissionStore.selectedComboInfo);

	console.log(!uni.$utils.isEmpty(permissionStore.selectedComboInfo));
	if (!uni.$utils.isEmpty(permissionStore.selectedComboInfo)) {
		// 判断是否存在选中套餐的记录
		let oldSelectedComboInfo = uni.$utils.deepClone(permissionStore.selectedComboInfo);
		console.log(oldSelectedComboInfo);
		console.log(comboList.value);

		let findData = comboList.value.find((item : any) => item.ModuleID == oldSelectedComboInfo.ModuleID);
		if (!uni.$utils.isEmpty(findData)) {
			// 于历史记录相匹配
			selectedComboInfo.value = uni.$utils.deepClone(findData);
			selectedCombo.value = selectedComboInfo.value.ModuleID;
		} else {
			// console.log(findData);
			// 不匹配
			selectedComboInfo.value = comboList.value[0];
			selectedCombo.value = comboList.value[0].ModuleID;
		}

	} else {
		selectedComboInfo.value = comboList.value[0];
		selectedCombo.value = comboList.value[0].ModuleID;
	}


	// 进入上次APP模块
	async function initAPPModule() {
		let str = permissionStore.activeModule;
		console.log(str);

		if (!uni.$utils.isEmpty(permissionStore.activeModule)) {
			let module = str; // 上次选中APP模块
			console.log(module);
			uni.$utils.showLoading("正在加载历史选中模块！");

			let url = '';
			switch (module.ModuleID) {
				case 3: // qms
					uni.navigateTo({
						url: "/st_qms/basicPage/basicPage"
					})
					break;
				case 2:

					uni.navigateTo({
						url: "/st_ticket/basicPage/basicPage",
						success: (ret) => {
							uni.$utils.hideLoading()
						},
						fail(err) {
							uni.$utils.hideLoading()
						}
					});
					break;
				case 6:
				case 12:
				case 13:
					let homePage = module.authPages.find((item : any) => item.IsHomepage);
					if (homePage && homePage != undefined) {
						url = homePage.AP_Link; // 设置首页路径
						uni.navigateTo({
							url: url
						})
					} else {
						uni.showToast({
							icon: 'none',
							title: '当前应用未设置首页!'
						})
					}
					break;
				case 1:
					console.log(module);
					url = '/commonPage/Module/index' + '?moduleID=' + module.ModuleID + '&index=0';
					console.log(url);
					uni.navigateTo({
						url: url,
						success: (ret) => {
							console.log(ret);
						},
						fail(err) {
							console.log(err);
						}
					})
					break;
				default:
					break;
			}

			setTimeout(() => {
				uni.$utils.hideLoading();
			}, 1000);
		} else {
			uni.$utils.hideLoading();
		}
	};




	// onLoad((option : any) => {
	let activeModuleStartType = permissionStore.activeModuleStartType;
	console.log(activeModuleStartType);
	// 判断是否开机自启
	if (activeModuleStartType && process.env.NODE_ENV == 'development') {
		setTimeout(() => {
			initAPPModule();
		}, 1000);
	} else {
		uni.$utils.hideLoading();
	}
	// });
</script>


<style lang="scss" scoped>
	$h-padding: 5px;
	$v-padding: 10px;
	$radius: 5px;

	.comboSelect {
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: 16px;
		font-weight: 500;
		width: 185px;
		border-radius: $radius;

		::v-deep .uni-stat-box {
			background-color: transparent !important;
		}
	}

	.boardSelect {
		display: flex;
		align-items: center;
		margin-left: 15px;
		padding: 0 10px;
		height: 35px;
		font-size: 16px;
		font-weight: 500;
		background: rgba(0, 0, 0, .03);
		border-radius: $radius;

		.text {
			color: #fff;
			font-size: 14px;
			margin-right: 5px
		}
	}

	// 版本选择下拉
	.comboSelect ::v-deep .uni-select__input-text {
		font-size: 14px;
		color: #fff;
		background-color: transparent !important;
	}

	.comboSelect ::v-deep .uni-select {
		border: none;
		background-color: transparent !important;
	}

	.page__content {
		display: flex;
		flex-direction: column;
		background-color: #f8f8f8;
		// padding-left: $h-padding;
		// padding-right: $h-padding;

		.head {
			position: fixed;
			left: 0;
			top: 0;
			display: flex;
			flex-direction: column;
			justify-content: center;
			align-items: center;
			padding: calc(50px + #{$v-padding}) $h-padding $v-padding $h-padding;
			z-index: 2;
			width: 100vw;
			height: 150px;
			background-color: $main-background-color;

			&-top {
				width: 100%;
				display: flex;
				justify-content: space-between;
				align-items: center;

				&-left {
					display: flex;
					align-items: center;
					width: 350rpx;
					color: #fff;

					&-text {
						font-size: 32rpx;
						margin-right: 10rpx;
					}
				}
			}

			&-bottom {
				width: 100%;
				margin-top: $v-padding;
			}
		}


		.subcontent {
			display: flex;
			flex-direction: column;
			// padding: calc(50px + env(safe-area-inset-top) + #{$v-padding}) $h-padding $v-padding $h-padding;

			.commonBox {
				display: flex;
				flex-direction: column;
				padding: $v-padding $h-padding;
				background-color: #ffffff;
				margin-bottom: $v-padding;
				border-radius: 15rpx;

				&-title {
					display: flex;
					align-items: center;
					justify-content: space-between;
					padding: 0 $h-padding $v-padding $h-padding;

					&-left {
						display: flex;
						justify-content: flex-start;
						align-items: center;
						width: 150rpx;
					}

					&-right {
						display: flex;
						justify-content: flex-end;
						align-items: center;
						width: 100rpx;
					}
				}

				&-content {
					display: flex;
					flex-direction: column;
				}
			}
		}
	}

	.grid-text {
		font-size: 14px;
		color: #909399;
		padding: 10rpx 0 20rpx 0rpx;
		/* #ifndef APP-PLUS */
		box-sizing: border-box;
		/* #endif */
	}

	.shortcut {
		display: flex;
		flex-direction: column;
		height: 80vh;

		&-content {
			display: flex;
			flex: 1;
			padding: $v-padding $h-padding;
			background-color: #f7f7f7;

			.left_wrap {
				display: flex;
				flex-direction: column;
				width: 180rpx;

				.leftItem {
					display: flex;
					justify-content: center;
					align-items: center;
					height: 60rpx;
				}

				.leftItem.on {
					background-color: $main-background-color;
					color: #ffffff;
				}
			}

			.right_wrap {
				display: flex;
				flex-direction: column;
				flex: 1;

				.rightItem {
					display: flex;
					justify-content: flex-start;
					align-items: center;
					font-size: 28rpx;
					margin-left: $h-padding;
				}

				.rightItem.on {
					background-color: $main-background-color;
					color: #ffffff;
				}
			}
		}

		&-bottom {
			display: flex;
			justify-content: center;
			align-items: center;
			position: fixed;
			width: 100%;
			height: 100rpx;
			padding: $v-padding $h-padding;
			left: 0;
			bottom: calc(100rpx + env(safe-area-inset-bottom) / 2);
		}
	}
</style>